﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace TLWinReplace
{
    public partial class AddPartitionForm : Form
    {
        private PartitionElement m_pe;

        public PartitionElement PE
        {
            get { return m_pe; }
            set { m_pe = value; }
        }

        public AddPartitionForm()
        {
            InitializeComponent();

            // initial member
            m_pe = new PartitionElement();
            m_pe.PartType = "Header";
            m_pe.StPartHex = "0";
            m_pe.StDataHex = "0";
            m_pe.LengthDec = "0";
            m_pe.DataType = "0xFF";
            m_pe.Other = "";
            textBox1.Enabled = false;
            button3.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if ("0" == m_pe.LengthDec)
            {
                MessageBox.Show("You must give a partition larger than 0!");
                return;
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            m_pe.PartType = comboBox1.Text;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            m_pe.LengthDec = textBox2.Text;
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            m_pe.StPartHex = textBox3.Text;
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            m_pe.StDataHex = textBox4.Text;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked == Enabled)
            {
                m_pe.DataType = "0xFF";
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked == Enabled)
            {
                m_pe.DataType = "0x00";
            }
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton3.Checked == Enabled)
            {
                m_pe.DataType = "random";
            }
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton4.Checked == Enabled)
            {
                m_pe.DataType = "XOR";
            }
        }

        private void radioButton6_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton6.Checked == Enabled)
            {
                m_pe.DataType = "header";
                textBox2.Text = "2048";
                m_pe.LengthDec = textBox2.Text;
            }
        }

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton5.Checked == Enabled)
            {
                m_pe.DataType = "FromFile";
                textBox1.Enabled = true;
                button3.Enabled = true;
            }
            else
            {
                textBox1.Enabled = false;
                button3.Enabled = false;
                textBox1.Text = "";
                m_pe.Other = "";
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog openfiledialog = new OpenFileDialog();
            openfiledialog.Filter = "Data File (*.*) | *.*";
            openfiledialog.FilterIndex = 0;
            openfiledialog.RestoreDirectory = true;
            // openfiledialog.CreatePrompt = true;
            openfiledialog.Title = "Select Data File";
            openfiledialog.ShowDialog();

            if (openfiledialog.FileName.IndexOf(":") < 0)
            {   // if click cancel
                return;
            }

            // get the file full directory
            string str;
            str = openfiledialog.FileName.ToString();
            textBox1.Text = str;
            m_pe.Other = str;

            //string filedir = dataGridView1[5, (int)i].Value.ToString();
            FileInfo info = new FileInfo(str);
            textBox2.Text = (info.Length / 512).ToString();
            m_pe.LengthDec = textBox2.Text;
        }
    }
}
